package article_new

import (
	"github.com/jinzhu/gorm"
)

type ArticleNew struct {
	Title         string `json:"title"`
	Desc          string `json:"desc"`
	Content       string `json:"content"`
	CoverImageUrl string `json:"cover_image_url"`
	CreatedBy     string `json:"created_by"`
	ModifiedBy    string `json:"modified_by"`
	State         int    `json:"state"`
	Id            int    `json:"id"`
	Uuid          string `json:"uuid"`
}

func (m *ArticleNew) TableName() string {
	return "article"
}

// ExistArticleByID checks if an article exists based on ID
func ExistArticleByID(id int) (bool, error) {
	var article ArticleNew
	err := db.Select("id").Where("id = ? AND is_deleted = ? ", id, 0).First(&article).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}

	if article.Id > 0 {
		return true, nil
	}

	return false, nil
}

func Get(page int, size int) (articles []ArticleNew, err error, count int) {
	offest := (page - 1) * size
	err = db.Table("article").Select("id,title,uuid").Where("is_deleted = 0").
		Order("id desc").Offset(offest).Limit(size).Find(&articles).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return articles, err, 0
	}
	db.Table("article").Select("id").Count(&count)

	return articles, nil, count
}
